Valid for Sitecore
5.3.1
Item version's creation date/time
Q:
How is the item version creation date/time stored?
A:
Sitecore stores each item version's creation date/time in a field named __created (Sitecore.FieldIDs.Created). Most users cannot update this field value. One solution to store the item's creation date in addition to the version's, and to allow the user to adjust this value, involves NVelocity (http://sdn.sitecore.net/Articles/API/Using%20NVelocity/Set%20Default%20Date.html). Another involves implementing an event handler (http://sdn.sitecore.net/Articles/API/Using%20Events.html).
To implement an event handler to store the item's creation date, update the namespace and class name before compiling the code below into a Visual Studio project. Register the handler for the item:created event in web.config, where FieldName indicates the name of the field to populate with the item’s creation date:
<event name="item:created">
<handler type="Global.Events.ItemEventHandler, Global" method="OnItemCreated">
<FieldName>ItemCreationDate</FieldName>
</handler>
</event>
using System;
namespace Global.Events
{
public class ItemEventHandler
{
private string _fieldName = null;
public string FieldName
{
get { return (_fieldName); }
set { _fieldName = value; }
}
public void OnItemCreated( object Sender, EventArgs args )
{
Sitecore.Diagnostics.Assert.IsNotNull(_fieldName, "Field name not provided" );
Sitecore.Data.Events.ItemCreatedEventArgs cArgs = Sitecore.Events.Event.ExtractParameter(args, 0) as Sitecore.Data.Events.ItemCreatedEventArgs;
if (cArgs != null && cArgs.Item != null && cArgs.Item.Fields[FieldName] != null && String.IsNullOrEmpty(cArgs.Item.Fields[FieldName].Value))
{
cArgs.Item.Editing.BeginEdit();
cArgs.Item.Fields[FieldName].Value = Sitecore.DateUtil.IsoNow;
cArgs.Item.Editing.EndEdit();
}
}
}
}